<?php
namespace app\admin\inter_logic;

use app\inter\model\AreaModel;
use app\admin\inter_model\CarBanModel;
use app\admin\inter_model\CarRouteModel;
use think\Db;
class CarLogic
{
    public function searchCarList($map,$field='a.*',$flag = true )
    {
        return $flag ? Db::connect(config('intercity_db'))->name('car_list a')->join('driver b','b.id=a.driver','LEFT')
            ->where($map)->field($field)->select()
            : Db::connect(config('intercity_db'))->name('car_list a')->join('driver b','b.id=a.driver','LEFT')
                ->where($map)->column($field);
    }

    /**
     * 根据id获取汽车信息
     * @param $id
     * @param $field
     * @param $type //1为汽车id，2为司机id
     * @return array
     * */
    public function getCarInfoById($id,$field='a.*',$type=1)
    {
        $type == 1 ? $map['a.id'] = $id : $map['driver'] = $id;
        return Db::connect(config('intercity_db'))->name('car_list a')->join(['xm_driver_base'=>'b'],'b.driver_id=a.driver','LEFT')
            ->where($map)->field($field)->find();
    }

    /**
     * 根据条件获取运营地区信息
     * @param $map
     * @param $field
     * @param $page
     * @param $limit
     * @return array|false
     * */
    public function getArea($map,$field='*',$page=1,$limit=10)
    {
        $model = new AreaModel();
        return $model->getAreaInfo($map,$field,true,$page,$limit);
    }

    /**
     * 根据id获取运营地区信息
     * @param $id
     * @param $field
     * @return array|false
     * */
    public function getAreaById($id,$field='*')
    {
        $model = new AreaModel();
        return $model->searchArean(array('id'=>$id),$field);
    }

    /**
     * 根据条件获取班次信息
     * @param $map
     * @param $field
     * @param $page
     * @param $limit
     * @return array|false
     * */
    public function getBanInfo($map,$field='cb.*',$page=1,$limit=10)
    {
        $model = new CarBanModel();
        return $model->getBanciList($map,$field,$page,$limit);
    }

    /**
     * 根据条件获取线路信息
     * @param $map
     * @param $field
     * @param $page
     * @param $limit
     * @return array|false
     * */
    public function getRouteInfo($map,$field='cr.*',$page=1,$limit=10)
    {
        $model = new CarRouteModel();
        return $model->getRouteList($map,$field,$page,$limit);
    }

    /**
     * 根据id获取班次信息
     * @param $id
     * @param $field
     * @return array|false
     * */
    public function getBanById($id,$field='*')
    {
        $model = new CarBanModel();
        return $model->searchBanci(array('id'=>$id),$field);
    }

    /**
     * 根据id获取路线信息
     * @param $id
     * @param $field
     * @return array|false
     * */
    public function getRouteById($id,$field='*')
    {
        $model = new CarRouteModel();
        return $model->searchRoute(array('id'=>$id),$field);
    }

    /**
     * 更新班次信息
     * @param $map
     * @param $data
     * @return array|false
     * */
    public function renewBan($map,$data)
    {
        if(empty($data))return false;
        $model = new CarBanModel();
        return $model->updateBanci($map,$data);
    }

    /**
     * 更新路线信息
     * @param $map
     * @param $data
     * @return array|false
     * */
    public function renewRoute($map,$data)
    {
        if(empty($data))return false;
        $model = new CarRouteModel();
        return $model->updateRoute($map,$data);
    }


    /**
     * 添加班次信息
     * @param $data
     * @return string|false
     * */
    public function createRoute($data)
    {
        if(empty($data))return false;
        $model = new CarRouteModel();
        return $model->addRoute($data);
    }

    /**
     * 添加班次信息
     * @param $data
     * @return string|false
     * */
    public function createBan($data)
    {
        if(empty($data))return false;
        $model = new CarBanModel();
        return $model->addBanci($data);
    }

    /**
     * 根据条件删除班次
     * @param $map
     * @return string|false
     * */
    public function delBan($map)
    {
        if(empty($map))return false;
        $model = new CarBanModel();
        return $model->deleteBanci($map);
    }

    /**
     * 根据条件删除路线
     * @param $map
     * @return string|false
     * */
    public function delRoute($map)
    {
        if(empty($map))return false;
        $model = new CarRouteModel();
        return $model->deleteRoute($map);
    }

    /**
     * 根据id获取司机、车辆的详细信息
     * @param $id
     * @param $field
     * @param $type //1为车辆id，2为司机id
     * @return array|false
     * */
    public function getDetailAllById($id,$field='a.*',$type=1)
    {
        switch($type){
            case 1:
                $map['a.car_id'] = $id;
                break;
            case 2:
                $map['b.driver_id'] = $id;
                break;
            default:
                $map = array();
                break;
        }
        return Db::table('xm_car_info a')->join(['xm_driver_base'=>'b'],'a.driver_id=b.driver_id','LEFT')
            ->join(['xm_driver_info'=>'e'], 'e.driver_id = b.driver_id', 'LEFT')
            ->join(['inter_car_tran'=>'c'],'a.car_id=c.car_id','LEFT')->join(['inter_car_insurance_tran'=>'d'],'a.car_id=d.car_id','LEFT')
            ->where($map)->field($field)->find();
    }

}
